package com.ruoyi.device.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.device.domain.DeviceCollectConfig;
import com.ruoyi.device.service.IDeviceCollectConfigService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 设备采集配置Controller
 * 
 * @author ruoyi
 * @date 2025-10-20
 */
@RestController
@RequestMapping("/collect")
public class DeviceCollectConfigController extends BaseController
{
    @Autowired
    private IDeviceCollectConfigService deviceCollectConfigService;

    /**
     * 查询设备采集配置列表
     */
    @RequiresPermissions("device:collect:list")
    @GetMapping("/list")
    public TableDataInfo list(DeviceCollectConfig deviceCollectConfig)
    {
        startPage();
        List<DeviceCollectConfig> list = deviceCollectConfigService.selectDeviceCollectConfigList(deviceCollectConfig);
        return getDataTable(list);
    }

    /**
     * 导出设备采集配置列表
     */
    @RequiresPermissions("device:collect:export")
    @Log(title = "设备采集配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceCollectConfig deviceCollectConfig)
    {
        List<DeviceCollectConfig> list = deviceCollectConfigService.selectDeviceCollectConfigList(deviceCollectConfig);
        ExcelUtil<DeviceCollectConfig> util = new ExcelUtil<DeviceCollectConfig>(DeviceCollectConfig.class);
        util.exportExcel(response, list, "设备采集配置数据");
    }

    /**
     * 获取设备采集配置详细信息
     */
    @RequiresPermissions("device:collect:query")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable("configId") Long configId)
    {
        return success(deviceCollectConfigService.selectDeviceCollectConfigByConfigId(configId));
    }

    /**
     * 新增设备采集配置
     */
    @RequiresPermissions("device:collect:add")
    @Log(title = "设备采集配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceCollectConfig deviceCollectConfig)
    {
        return toAjax(deviceCollectConfigService.insertDeviceCollectConfig(deviceCollectConfig));
    }

    /**
     * 修改设备采集配置
     */
    @RequiresPermissions("device:collect:edit")
    @Log(title = "设备采集配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceCollectConfig deviceCollectConfig)
    {
        return toAjax(deviceCollectConfigService.updateDeviceCollectConfig(deviceCollectConfig));
    }

    /**
     * 删除设备采集配置
     */
    @RequiresPermissions("device:collect:remove")
    @Log(title = "设备采集配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        return toAjax(deviceCollectConfigService.deleteDeviceCollectConfigByConfigIds(configIds));
    }

    /**
     * 启动采集任务
     */
    @RequiresPermissions("device:collect:start")
    @Log(title = "设备采集配置", businessType = BusinessType.UPDATE)
    @PostMapping("/start/{configId}")
    public AjaxResult start(@PathVariable Long configId)
    {
        int result = deviceCollectConfigService.startCollectTask(configId);
        if (result > 0)
        {
            return AjaxResult.success("采集任务已启动");
        }
        return AjaxResult.error("启动失败");
    }

    /**
     * 停止采集任务
     */
    @RequiresPermissions("device:collect:stop")
    @Log(title = "设备采集配置", businessType = BusinessType.UPDATE)
    @PostMapping("/stop/{configId}")
    public AjaxResult stop(@PathVariable Long configId)
    {
        int result = deviceCollectConfigService.stopCollectTask(configId);
        if (result > 0)
        {
            return AjaxResult.success("采集任务已停止");
        }
        return AjaxResult.error("停止失败");
    }

    /**
     * 手动执行一次采集
     */
    @RequiresPermissions("device:collect:execute")
    @Log(title = "设备采集配置", businessType = BusinessType.OTHER)
    @PostMapping("/execute/{configId}")
    public AjaxResult execute(@PathVariable Long configId)
    {
        try
        {
            deviceCollectConfigService.executeCollect(configId);
            return AjaxResult.success("采集任务已提交执行");
        }
        catch (Exception e)
        {
            return AjaxResult.error("执行失败: " + e.getMessage());
        }
    }
}

